package com.tqjc.business.interactivewithscan.core.thread;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tqjc.business.interactivewithscan.entity.ScanDeviceData;
import com.tqjc.business.interactivewithscan.entity.UfcDeviceInfo;
import com.tqjc.business.interactivewithscan.mapper.ScannerDeviceDataMapper;
import com.tqjc.business.interactivewithscan.mapper.UfcDeviceInfoMapper;
import com.tqjc.business.interactivewithscan.uils.EmptyUtil;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author binbinhan
 * @date 2021-06-16
 */
//@Configuration
public class DeviceThreadPool {
//
//    @Resource
//    ScannerDeviceDataMapper scannerDeviceDataMapper;
//
//    @Resource
//    private UfcDeviceInfoMapper deviceInfoMapper;
//
//    public static DevicesObject devices = new DevicesObject();
//
//    @PostConstruct
//    public void createThreadPool() {
//        LambdaQueryWrapper<ScanDeviceData> queryWrapper = new LambdaQueryWrapper<>();
//        //未停用，未删除
////        queryWrapper.eq(ScanDeviceData::getState, 0).eq(Device::getDel, 0);
//        queryWrapper.orderByDesc(ScanDeviceData::getId);
//        List<ScanDeviceData> deviceList = scannerDeviceDataMapper.selectList(queryWrapper);
//        Map<Long, UfcDeviceInfo> deviceInfoMap = new HashMap<>();
//        List<Integer> collect = deviceList.stream().map(ScanDeviceData::getId).collect(Collectors.toList());
//        if (!EmptyUtil.isEmpty(collect)) {
//            LambdaQueryWrapper<UfcDeviceInfo> wrapper = new LambdaQueryWrapper<>();
//            wrapper.in(UfcDeviceInfo::getDeviceOid, collect);
//            List<UfcDeviceInfo> infos = deviceInfoMapper.selectList(wrapper);
//            if (!EmptyUtil.isEmpty(infos)) {
//                deviceInfoMap = infos.stream().collect(Collectors.toMap(UfcDeviceInfo::getDeviceOid, o -> o));
//            }
//        }
//        for (ScanDeviceData device : deviceList) {
//            UfcDeviceInfo deviceInfo = deviceInfoMap.get(device.getId());
//            try {
//                if (device.getClassName() != null && !device.getClassName().equals("")) {
//                    Class<NormalThread> name = (Class<NormalThread>) Class.forName("com.tqjc.business.interactivewithscan.core.thread." + device.getClassName());
//                    NormalThread thread = name.newInstance();
//                    thread.setDevice(device, deviceInfo);
//                    devices.put(device.getDeviceCode(), thread);
//                    thread.start();
//                }
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InstantiationException e) {
//                e.printStackTrace();
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }
//
//
//    /**
//     * 每小时
//     */
////    @Scheduled(cron = "0 0 0/1 * * ?")
//    public void checkedDevice() {
//
//    }
}
